﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web
{
    public partial class Default : BasePage
    {
        IModulesBLL moduleBLL = IocContainer.Instance.Resolve<IModulesBLL>();
        protected int empId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            empId = IndividualId;
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                lblUserName.InnerText = UserName;
                LoadData();
            }
        }

        private void LoadData()
        {
            StringBuilder strb = new StringBuilder();
            DataTable dtAuthorityMenu = moduleBLL.GetModules(Convert.ToInt32(UserID), 0);
            DataTable dt = moduleBLL.GetModules();
            //DataRow[] drRoots = dtAuthorityMenu.Select("ParentModuleID = 0");
            //foreach (DataRow dr in drRoots)
            foreach (DataRow dr in dt.Rows)
            {
                string strModule = "<div class='accordionHeader'><h2><span>Folder</span>" + dr["ModuleName"].ToString() +
                                   "</h2></div>";

                StringBuilder childMenu = GetChildMenu(dtAuthorityMenu, Convert.ToInt32(dr["ModuleID"].ToString()));
                string childModule = childMenu.ToString();
                if (!string.IsNullOrEmpty(childModule))
                {
                    strb.Append(strModule);
                    strb.Append(childModule);
                }
                
            }
            MenuTree.Text = strb.ToString();
        }

        private StringBuilder GetChildMenu(DataTable dt,int parentId)
        {
            StringBuilder str = new StringBuilder();
            DataRow[] drRoots = dt.Select("ParentModuleID = " + parentId);
            if (drRoots.Length > 0)
            {
                str.Append("<div class='accordionContent'><ul class='tree treeFolder'>");
                foreach (DataRow dr in drRoots)
                {
                    str.AppendFormat("<li><a href='{0}' target='navTab' rel='dlg_page{1}'+ isIframe='true'>{2}</a></li>",
                        dr["ModuleURL"].ToString(), dr["ModuleID"].ToString(), dr["ModuleName"].ToString());
                    GetChildMenu(dt, Convert.ToInt32(dr["ModuleID"].ToString()));
                }
                str.Append("</ul></div>");
            }
            //else
            //{
            //    str.Append("<div class='accordionContent'><ul class='tree treeFolder'>");
            //    str.AppendFormat("<li></li>");
            //    str.Append("</ul></div>");
            //}
            return str;
        }


        [WebMethod]
        public static string GetMyToDoOrders(int empId)
        {
            IOrdersBLL ordersBLL = IocContainer.Instance.Resolve<IOrdersBLL>();
            string strResult;

            DataTable dtProducts = ordersBLL.GetOrdersToDo(empId);

            strResult = SerializationHelper.DataTable2Json(dtProducts);

            return strResult;

        }

    }
}
